package com.itheima.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("role")
public class RoleController {

    @Reference
    private RoleService roleService;


    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return roleService.findPage(queryPageBean);
    }

    /**
     * 新增
     */
    @RequestMapping("add")
    public Result add(@RequestBody Role role) {

        String keyword = role.getKeyword();
        keyword = "ROLE_" + keyword;
        role.setKeyword(keyword);

        try {
            roleService.add(role);
            return Result.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("");
        }
    }


    /**删除
     */
    @RequestMapping("deletebyid")
    public Result deletebyid(Integer id){
        try {
            roleService.deletebyid(id);
            return Result.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("");
        }
    }


    @RequestMapping("findbyid4updata")
    public Result findbyid4updata(Integer id){
        try {
            Map map= roleService.findbyid4updata(id);
            return Result.success("",map);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("");
        }
    }


    @RequestMapping("edit")
    public Result edit(@RequestBody Role role){
        try {
            String keyword = role.getKeyword();
            keyword = "ROLE_"+keyword;
            role.setKeyword(keyword);
            roleService.edit(role);
            return Result.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("");
        }
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        List<Role> roles = roleService.findAll();
        return Result.success("",roles);
    }



}
